package com.jsy.mini12306.ticketsales;

import com.jsy.mini12306.common.entity.*;
import com.jsy.mini12306.ticketsales.entity.TrainPatternSeatMap;
import com.jsy.mini12306.ticketsales.mapper.TicketMapper;
import com.jsy.mini12306.ticketsales.mapper.TrainCalendarMapper;
import com.jsy.mini12306.ticketsales.mapper.TrainPatternSeatMapMapper;
import com.jsy.mini12306.ticketsales.service.TrainService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Random;

/**
 * @author: SongyangJi
 * @description:
 * @since: 2021/9/3
 */

@SpringBootTest
public class GenerateData {

    @Resource
    TrainService trainService;

    @Resource
    TrainPatternSeatMapMapper trainPatternSeatMapMapper;

    @Resource
    TrainCalendarMapper calendarMapper;

    @Resource
    TicketMapper ticketMapper;




    // 生成train_id = 1、2 的座位分布图
    @Test
    void generateTrainPatternSeatMap_CRH380BG() {
        {
            int train_id = 1;

            SeatType[] seatTypes = {SeatType.BUSINESS, SeatType.FIRST, SeatType.SECOND};
            int[] ns = {2, 4, 10};
            int[] cols = {12, 15, 16}; // 每个车厢的排数
            List<List<String>> sts = List.of(List.of("A", "C", "F"), List.of("A", "C", "D", "F"), List.of("A", "B", "C", "D", "F"));
            int cnt = 0;
            int bin = 1; // 车号
            for (int i = 0; i < ns.length; i++) { // 三个种类
                int idx = 0;  // 索引号，  每个座位的位图各自生成, 注意从 0 开始
                List<String> ss = sts.get(i);
                for (int j = 0; j < ns[i]; j++) {
                    int col = 1; // 排数
                    for (int k = 0; k < cols[i]; k++) {
                        for (String s : ss) {
                            TrainPatternSeatMap map = new TrainPatternSeatMap(train_id, seatTypes[i], idx++, "" + bin, col + s);
                            System.out.println(map);
                            trainPatternSeatMapMapper.insert(map);
                            cnt++;
                        }
                        col++;
                    }
                    ++bin;
                }
            }
            System.out.println("座位数量: " + cnt);


        }

        {

            int train_id = 2;
            SeatType[] seatTypes = {SeatType.BUSINESS, SeatType.FIRST, SeatType.SECOND};
            int[] ns = {1, 2, 5};
            int[] cols = {12, 15, 16}; // 每个车厢的排数
            List<List<String>> sts = List.of(List.of("A", "C", "F"), List.of("A", "C", "D", "F"), List.of("A", "B", "C", "D", "F"));
            int cnt = 0;
            int bin = 1; // 车号
            for (int i = 0; i < ns.length; i++) { // 三个种类
                int idx = 0;  // 索引号，  每个座位的位图各自生成, 注意从 0 开始
                List<String> ss = sts.get(i);
                for (int j = 0; j < ns[i]; j++) {
                    int col = 1; // 排数
                    for (int k = 0; k < cols[i]; k++) {
                        for (String s : ss) {
                            TrainPatternSeatMap map = new TrainPatternSeatMap(train_id, seatTypes[i], idx++, "" + bin, col + s);
                            System.out.println(map);
                            trainPatternSeatMapMapper.insert(map);
                            cnt++;
                        }
                        col++;
                    }
                    ++bin;
                }
            }
            System.out.println("座位数量: " + cnt);
        }


    }


    // 生成train_id = 1、2 的运行日历
    @Test
    void generateTrainCalendar() {
        LocalDate today = LocalDate.parse("2021-09-07");

        for (int i = 0; i < 30; i++) {
            LocalDate date = today.plusDays(i);
            calendarMapper.insert(new TrainCalendar(1, date));
        }

        Random random = new Random();
        for (int i = 0; i < 30; i++) {
            LocalDate date = today.plusDays(i);
            if (random.nextBoolean()) {
                calendarMapper.insert(new TrainCalendar(2, date));
            }
        }

    }


    // 生成 train_id = 1、2的车票价格
    @Test
    void generateTickets() {
        int[] ids = {1, 2};
        Random random = new Random();
        SeatType[] seatTypes = {SeatType.BUSINESS, SeatType.FIRST, SeatType.SECOND};

        double[] factors = {1.0, 0.7, 0.5};


        for (int train_id : ids) {
            Train train = trainService.queryTrainById(train_id);
            for (int idx = 0; idx < seatTypes.length; idx++) {
                List<TrainPassStation> trainPassStations = train.getTrainPassStations();
                int len = trainPassStations.size();
                // 循环
                for (int i = 0; i < len; i++) {
                    int prePrice = 0;
                    for (int j = i + 1; j < len; j++) {
                        TrainPassStation trainPassStation1 = trainPassStations.get(i);
                        TrainPassStation trainPassStation2 = trainPassStations.get(j);
                        SeatType seatType = seatTypes[idx];
                        double factor = factors[idx];
                        int price = (int) (factor * (random.nextInt(800) + 200));
                        price = Math.max(prePrice, price);
                        Ticket ticket = new Ticket(train_id, seatType, trainPassStation1.getStationId(), trainPassStation2.getStationId(), (float) price);
                        ticketMapper.insert(ticket);
                    }
                }
            }
        }
    }


}
